package com.mine.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mine.pojo.MineGoods;
import com.mine.pojo.MineGoodsCategory;
import com.mine.pojo.MineUser;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.service.GoodsService;
import com.mine.service.UserService;
import com.mine.vo.GoodsQueryVo;

@Controller
public class GoodsController {

	@Autowired 
	GoodsService goodsService;
	
	@Autowired
	UserService userService;
	
	//添加商品
	@RequestMapping(value="/goods/add/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result addGoods(@PathVariable String token, @RequestBody MineGoods goods){
		if (goods==null||token==null||goods.getCategoryId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.addGoods(token,goods);
		return result;
	}
	
	//删除商品
	@RequestMapping(value="/goods/delete/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result deleteGoods(@PathVariable String token, @RequestBody MineGoods goods){
		if (goods==null||goods.getId()==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.deleteGoods(token,goods);
		return result;
	}
	//下架商品
	@RequestMapping(value="/goods/unshelve/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateGoodsUnshelve(@PathVariable String token, @RequestBody MineGoods goods){
		if (goods==null||goods.getId()==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.updateGoodsUnshelve(token,goods);
		return result;
	}
		
	//修改商品
	@RequestMapping(value="/goods/update/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateGoods(@PathVariable String token, @RequestBody MineGoods goods){
		if (goods==null||goods.getId()==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.updateGoods(token,goods);
		return result;
	}
	
	//查询商品
	@RequestMapping(value="/goods/getInfo/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result getGoodsInfo(@PathVariable String token, @RequestBody MineGoods goods){
		if (goods==null||goods.getId()==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.getGoodsInfo(token,goods);
		return result;
	}
	
	//获取商铺的所有商品列表，分页展示
	@RequestMapping("/goods/getGoodsList/{token}/{page}/{rows}")
    @ResponseBody
    public Result getGoodsList(@PathVariable String token,@PathVariable int page,@PathVariable int rows) {
		
		return goodsService.getGoodsList(token,page,rows);
		
	}
	
	//添加商品分类
	@RequestMapping(value="/goods/getManagerAllCategory/{token}",method=RequestMethod.GET)
	@ResponseBody
	public Result getManagerAllCategory(@PathVariable String token){
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.getManagerAllCategory(token);
		return result;
	}
	//添加商品分类
	@RequestMapping(value="/goods/category/add/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result addGoodsCategory(@PathVariable String token, @RequestBody MineGoodsCategory goodsCategory){
		if (goodsCategory==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.addGoodsCategory(token,goodsCategory);
		return result;
	}
	//修改商品分类
	@RequestMapping(value="/goods/category/update/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateGoodsCategory(@PathVariable String token, @RequestBody MineGoodsCategory goodsCategory){
		if (goodsCategory==null||token==null||goodsCategory.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.updateGoodsCategory(token,goodsCategory);
		return result;
	}
	//删除商品分类
	@RequestMapping(value="/goods/category/delete/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result deleteGoodsCategory(@PathVariable String token, @RequestBody MineGoodsCategory goodsCategory){
		if (goodsCategory==null||token==null||goodsCategory.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.deleteGoodsCategory(token,goodsCategory);
		return result;
	}
	//查询某一个商铺下面所有商品列表
	@RequestMapping(value="/manager/goods/all/{token}/{size}/{page}/{categoryId}")
	@ResponseBody
	public Result getGoodsAllByToken(@PathVariable String token,@PathVariable Integer size,@PathVariable Integer page,@PathVariable Long categoryId){
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，添加失败！");
		}
		
		//拼装查询条件
		GoodsQueryVo goodsQueryVo = new GoodsQueryVo();
		System.out.println(categoryId);
		//categoryId==-1查询所有 别的值以真是分类id查询
		if (categoryId.intValue()==-1) {
			goodsQueryVo.setCategoryId(null);
		}else{
			goodsQueryVo.setCategoryId(categoryId);
		}
		goodsQueryVo.setStatus(null);
		//goodsQueryVo.setCategoryId(categoryId);
		goodsQueryVo.setShopId(user.getShopId());
		if (size==null) {
			size =10;
		}
		goodsQueryVo.setSize(size);
		if (page==null) {
			page =1;
		}
		goodsQueryVo.setPage(page);
		goodsQueryVo.setStart((goodsQueryVo.getPage()-1)*goodsQueryVo.getSize());
		Result result = goodsService.getGoodsAll(goodsQueryVo);
		return result;
	}
/////////////////////////////////////////////////////查询////////////////////////////////////////////////////////////////////////////////////////////////////
	//查询某一个商铺下面的某一个分类对应的商品列表，分页展示
	@RequestMapping(value="/goods/categoryList/{shopId}/{categoryId}/{size}/{page}")
	@ResponseBody
	public Result getGoodsByCategoryId(@PathVariable Long shopId,@PathVariable Long categoryId,@PathVariable Integer size,@PathVariable Integer page){
		if (shopId==null||categoryId==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		//拼装查询条件
		GoodsQueryVo goodsQueryVo = new GoodsQueryVo();
		goodsQueryVo.setCategoryId(categoryId);
		goodsQueryVo.setShopId(shopId);
		if (size==null) {
			size =10;
		}
		goodsQueryVo.setSize(size);
		if (page==null) {
			page =1;
		}
		goodsQueryVo.setPage(page);
		goodsQueryVo.setStart((goodsQueryVo.getPage()-1)*goodsQueryVo.getSize());
		Result result = goodsService.getGoodsByCategoryId(goodsQueryVo);
		return result;
	}
	//查询某一个商铺下面所有商品列表
	@RequestMapping(value="/goods/all/{shopId}/{size}/{page}")
	@ResponseBody
	public Result getGoodsAll(@PathVariable Long shopId,@PathVariable Integer size,@PathVariable Integer page){
		if (shopId==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		//拼装查询条件
		GoodsQueryVo goodsQueryVo = new GoodsQueryVo();
		//goodsQueryVo.setCategoryId(categoryId);
		goodsQueryVo.setShopId(shopId);
		if (size==null) {
			size =10;
		}
		goodsQueryVo.setSize(size);
		if (page==null) {
			page =1;
		}
		goodsQueryVo.setPage(page);
		goodsQueryVo.setStart((goodsQueryVo.getPage()-1)*goodsQueryVo.getSize());
		Result result = goodsService.getGoodsAll(goodsQueryVo);
		return result;
	}
	//查询某一个商铺下面所有分类列表
	@RequestMapping(value="/goods/allCategory/{shopId}")
	@ResponseBody
	public Result getGoodsAllCategory(@PathVariable Long shopId){
		if (shopId==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.getGoodsAllCategory(shopId);
		return result;
	}
	//根据商品id查询商品详情
	@RequestMapping(value="/goods/detail/{goodsId}")
	@ResponseBody
	public Result getGoodsDetail(@PathVariable Long goodsId){
		if (goodsId==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = goodsService.getGoodsDetail(goodsId);
		return result;
	}
}
